package com.bjpowernode.redis;

import com.bjpowernode.utils.RedisUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Transaction;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 使用Jedis操作Rdis的思路:
 *      使用Jedis对象来调用相关的方法操作redis
 *
 * 使用JedisPool连接池操作Reids的思路
 *        1. 先获取连接池对象
 *        2. 从连接池对象中获取一个Jedis对象
 *        3. 拿到Jedis对象后就可以操作redis了
 *
 *
 */
public class JedisPoolDemo {

    public static void main(String[] args) {
        //1. 先获取连接池对象
        JedisPool open = RedisUtils.open("192.168.58.111", 6379);
        //2. 从连接池对象中获取一个Jedis对象
        Jedis jedis = open.getResource();
        //3. 拿到Jedis对象后就可以操作redis了
//        jedis.hset("student","id","1001");
//        jedis.hset("student","name","zhangsan");
//        jedis.hset("student","age","18");
//
//        String name = jedis.hget("student", "name");
//        System.out.println(name);
//        jedis.flushDB();
//        Map<String,String> maps = new HashMap<String,String>();
//        maps.put("id","1002");
//        maps.put("name","lisi");
//        maps.put("age","19");
//        jedis.hmset("student",maps);
//        List<String> hmget = jedis.hmget("student", "id", "name", "age");
//        for (String s : hmget) {
//            System.out.println(s);
//        }

        // 事务的实现
        Transaction multi = jedis.multi();
        multi.set("str1","aaa");
        multi.set("str2","bbb");
        List<Object> exec = multi.exec();
        for (Object o : exec) {
            System.out.println(o);
        }

        open.close();
    }


}
